package com.yuke.cloud.service.smc.controller.frontend;

import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.pmc.dto.ProdCategoryDto;
import com.yuke.cloud.service.smc.dto.*;
import com.yuke.cloud.service.smc.entity.Shop;
import com.yuke.cloud.service.smc.entity.ShopGoods;
import com.yuke.cloud.service.smc.service.ShopGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by wg on 2018/09/10.
*/
@Slf4j
@Api(value = "WEB - ShopGoodsController", tags = "处理ShopGoods相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/shop/goods")
public class ShopGoodsController extends BaseController {
    @Resource
    private ShopGoodsService shopGoodsService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "shopGoods", value = "该数据记录的POJO") @RequestBody ShopGoods shopGoods) {
        shopGoodsService.save(shopGoods);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        shopGoodsService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "shopGoods", value = "需要修改的内容") @RequestBody ShopGoods shopGoods) {
        shopGoodsService.update(shopGoods);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<ShopGoods> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        ShopGoods shopGoods = shopGoodsService.findById(id);
        return WrapMapper.ok(shopGoods);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<ShopGoods>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ShopGoods> list = shopGoodsService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    //通过spec_id,longitude,latitude获取商品店铺等详情
    /*@GetMapping("/app/spec/{specId}")
    @ApiOperation(httpMethod = "GET", value = "根据具体商品规格ID获取所有售卖该商品的店铺以及货物信息")
    public Wrapper<List<ShopAndGoodsInfoDto>> getShopAndGoodsBySpecId(@ApiParam(name = "specId", value = "specId") @PathVariable Long specId,
                                                               @RequestParam(value = "longitude", defaultValue = "") String longitude,
                                                               @RequestParam(value = "latitude", defaultValue = "") String latitude) {
        List<ShopAndGoodsInfoDto> shopAndGoodsInfoDtos = shopGoodsService.getShopAndGoodsBySpecId(specId, longitude, latitude);
        return WrapMapper.ok(shopAndGoodsInfoDtos);
    }

    @PostMapping("/app/search")
    @ApiOperation(httpMethod = "POST", value = "搜索栏搜索商品")
    public Wrapper<List<ShopAndGoodsInfoDto>> getShopAndGoodsBySearch(@ApiParam(name = "searchDto", value = "search搜索名称") @RequestBody SearchDto search) {
        List<ShopAndGoodsInfoDto> shopAndGoodsInfoDtos = shopGoodsService.getShopAndGoodsBySearch(search.getSearch(), search.getLongitude(), search.getLatitude());
        return WrapMapper.ok(shopAndGoodsInfoDtos);
    }

    @GetMapping("/app/shop/{shopId}")
    @ApiOperation(httpMethod = "GET", value = "根据店铺ID获取该店铺所售卖的商品")
    public Wrapper<ShopGoodsInfoDto> getShopGoodsByShopId(@ApiParam(name = "shopId", value = "shopId") @PathVariable Long shopId) {
        //TODO:后期不一次返回所有数据
        ShopGoodsInfoDto shopGoodsInfoDto = shopGoodsService.getShopGoodsInfoByShopId(shopId);
        return WrapMapper.ok(shopGoodsInfoDto);
    }

    @GetMapping("/app/shop/category/{shopId}")
    @ApiOperation(httpMethod = "GET", value = "根据店铺ID获取该店铺所售卖商品的分类")
    public Wrapper<List<ProdCategoryDto>> getShopCategoryByShopId(@ApiParam(name = "shopId", value = "shopId") @PathVariable Long shopId) {
        List<ProdCategoryDto> prodCategoryDtos = shopGoodsService.getShopCategoryByShopId(shopId);
        return WrapMapper.ok(prodCategoryDtos);
    }

    @GetMapping("/app/shop/{shopId}/{categoryId}")
    @ApiOperation(httpMethod = "GET", value = "根据店铺ID获取该店铺所售卖商品分类下的商品")
    public Wrapper<List<GoodsInfoDto>> getShopGoodsByShopIdAndCategoryId(@ApiParam(name = "shopId", value = "shopId") @PathVariable Long shopId,
                                                                            @ApiParam(name = "categoryId", value = "categoryId") @PathVariable Long categoryId) {
        List<GoodsInfoDto> goodsInfoDtos = shopGoodsService.getShopGoodsByShopIdAndCategoryId(shopId, categoryId);
        return WrapMapper.ok(goodsInfoDtos);
    }*/

    @GetMapping("/app/shop/category/all/{shopId}")
    @ApiOperation(httpMethod = "GET", value = "根据店铺ID获取该店铺所售卖商品的分类")
    public Wrapper<List<ProdCategoryDto>> getShopCategoryByShopId(@ApiParam(name = "shopId", value = "shopId") @PathVariable Long shopId) {
        List<ProdCategoryDto> prodCategoryDtos = shopGoodsService.getShopCategoryAllByShopId(shopId);
        return WrapMapper.ok(prodCategoryDtos);
    }

    @PostMapping("/stock")
    @ApiOperation(httpMethod = "POST", value = "后台管理，根据店铺条件获取该店铺的库存")
    public Wrapper<PageInfo<StockGoodsDto>> getStockGoodsByCondition(@RequestBody ShopGoods shopGoods,
                                                                 @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                 @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        return WrapMapper.ok(shopGoodsService.getStockGoodsByCondition(shopGoods, page, size));
    }

    @GetMapping({"/app/stock/{shopId}/{categoryId}", "/app/stock/{shopId}/{categoryId}/{isAlive}"})
    @ApiOperation(httpMethod = "GET", value = "根据店铺ID和分类获取该店铺的库存")
    public Wrapper<List<StockGoodsDto>> getStockGoodsByCategoryIdAndShopId(@ApiParam(name = "shopId", value = "shopId") @PathVariable Long shopId,
                                                                           @ApiParam(name = "categoryId", value = "categoryId") @PathVariable Long categoryId,
                                                                           @ApiParam(name = "isAlive", value = "isAlive商品上下架状态") @PathVariable(required = false) Integer isAlive) {
        List<StockGoodsDto> list = shopGoodsService.getStockGoodsByCategoryIdAndShopId(shopId, categoryId, isAlive);
        return WrapMapper.ok(list);
    }

    @PostMapping("/app/apply/{shopId}/{specId}")
    @ApiOperation(httpMethod = "POST",value = "申请货物销售")
    public Wrapper applyGoods(@ApiParam(name = "shopId",value = "申请商品的shopId" ) @PathVariable Long shopId,
                              @ApiParam(name = "specId",value = "申请商品的specId" ) @PathVariable Long specId){
        shopGoodsService.applyGoods(shopId, specId);
        return WrapMapper.ok();
    }

    @PutMapping("/app/price")
    @ApiOperation(httpMethod = "PUT",value = "商品定价")
    public Wrapper goodPrice(@ApiParam(name = "shopGoodsId",value = "店铺商品表id") @RequestParam(defaultValue = "0") Long shopGoodsId,
                                @ApiParam(name = "salePrice",value = "自定义售价") @RequestParam(defaultValue = "0") Integer salePrice) {
        shopGoodsService.updateGoodsPrice(shopGoodsId,salePrice);
        return WrapMapper.ok();
    }

    @PostMapping("/app/isAlive/{isAlive}")
    @ApiOperation(httpMethod = "POST",value = "商品上下架")
    public Wrapper goodIsAlive(@ApiParam(name = "shopGoodsId",value = "店铺商品表id") @RequestBody String shopGoodsIds,
                               @ApiParam(name = "isAlive",value = "商品上下架") @PathVariable Integer isAlive) {
        shopGoodsService.goodsIsAlive(shopGoodsIds, isAlive);
        return WrapMapper.ok();
    }

    @PostMapping("/condition")
    @ApiOperation(httpMethod = "POST", value = "店铺商品审核,根据店铺状态为1：已申请贩卖，正在审核中，店铺名称或商品名称获取店铺商品")
    public Wrapper<PageInfo<ShopGoods>> getListByCondition(@ApiParam(name = "id", value = "记录id") @RequestBody(required = false) ShopGoods shopGoods,
                                                      @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                      @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ShopGoods> list = shopGoodsService.getListByCondition(shopGoods);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }
}
